﻿//TODO: HorizontalIntensity: implement and Test

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Histograms;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Tries to remove all shadows.
    /// </summary>
    public class HorizontalIntensity : BaseForeBackgroundColorFilter
    {
        protected Statistics[] m_aStatistic = null;

        protected bool m_bIncludeStatistics = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="HorizontalIntensity"/> class.
        /// </summary>
        public HorizontalIntensity()
        {
            M_PROCESSING_MODE = PROCESSING_MODE.ColorCount;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="HorizontalIntensity"/> class.
        /// </summary>
        /// <param name="_HorizontalIntensity">The _HorizontalIntensity.</param>
        internal HorizontalIntensity(HorizontalIntensity _HorizontalIntensity)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            int nHeight = _bitmap.Height;
            int nWidth = _bitmap.Width;

            m_aStatistic = new Statistics[nHeight];

            int x = 0;
            int y = 0;

            for (y = 0; y < nHeight; y++)
            {
                m_aStatistic[y] = new Statistics(_bitmap, new Rectangle(0, y, nWidth, 1), 1, 1);
                float fValue = 0;
                switch (M_PROCESSING_MODE)
                {
                    case PROCESSING_MODE.ColorCount:
                        fValue = m_aStatistic[y].Count;
                        break;

                    case PROCESSING_MODE.GrayValue:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].GrayValue;
                        } 
                        break;

                    case PROCESSING_MODE.R:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].R;
                        } 
                        break;

                    case PROCESSING_MODE.G:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].G;
                        }
                        break;

                    case PROCESSING_MODE.B:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].B;
                        }
                        break;

                    case PROCESSING_MODE.Hue:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].Hue;
                        }
                        break;

                    case PROCESSING_MODE.Luminance:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].Luminance;
                        }
                        break;

                    case PROCESSING_MODE.Brightness:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].Brightness;
                        }
                        break;

                    case PROCESSING_MODE.Saturation:
                        for (x = 0; x < nWidth; x++)
                        {
                            fValue += m_aStatistic[y][x].Saturation;
                        }
                        break;

                }
                int nValue = (int)fValue;
                for (x = 0; x < nValue; x++)
                {
                    _bitmap.SetPixel(x, y, m_ColorBackground);
                }
                for (x = nValue; x < nWidth; x++)
                {
                    _bitmap.SetPixel(x, y, m_ColorForeground);
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Gets statistics
        /// </summary>
        public Statistics[] Statistics
        {
            get { return m_aStatistic; }
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new HorizontalIntensity(this);
        }
    }
}
